﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AmkeniCrossLoginApplication.Data_Access;

namespace AmkeniCrossLoginApplication.Amkeni_System.CO.Edit
{
    public partial class AddIPMetadata : System.Web.UI.Page
    {
        Amkeni_DatabaseEntities db = new Amkeni_DatabaseEntities();
        
        int ipID;
        int rfpID;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                {
                    ipID = int.Parse(Request.QueryString["ipID"]);
                    rfpID = int.Parse(Request.QueryString["rfpID"]);
                }
            }
            catch (System.ArgumentNullException ex)
            {
                Response.Redirect("~/Amkeni_System/CO/ViewListOfIPs.aspx", true);
            }
        }

        /*protected void RFPList_SelectedIndexChanged(object sender, EventArgs e)
        {
            int Rfp_id = int.Parse(RFPList.SelectedValue.Trim());

            var selectedIPs = from data in db.implementing_partner_metadata
                              where data.RFP_id == Rfp_id
                              select new
                              {
                                  data.implementing_partner.ip_id,
                                  data.implementing_partner.abbreviation
                              };

            IPList.DataSource = selectedIPs.ToList();
            IPList.DataBind();
        }

        protected void IPList_SelectedIndexChanged(object sender, EventArgs e)
        {
            ContractFileUpload.ContextKeys = IPList.SelectedItem.Text.Trim();
        }

        protected void IPList_DataBound(object sender, EventArgs e)
        {
            DropDownList ddl = sender as DropDownList;
            int x;
            if (ddl != null)
            {
                foreach (ListItem li in ddl.Items)
                {
                    if (int.TryParse(li.Value.Trim(), out x))
                    {
                        int id = int.Parse(li.Value.Trim());
                        string name = (from data in db.implementing_partner
                                       where data.ip_id == id
                                       select data.name).FirstOrDefault();
                        li.Attributes["title"] = name;
                    }
                }
            }
        }*/

        protected void ContractFileUpload_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
        {
            //Threading.Sleep is invoked to cause the delay so we can see the progress image when the file is uploaded
            System.Threading.Thread.Sleep(5000);

            string defaultDirectory = "~/Document_Upload/";

            var ipMetadata = (from data in db.implementing_partner_metadata
                              where data.ip_id == ipID && data.RFP_id == rfpID
                              select data).Single();

            string createDirectory = ipMetadata.RFP.RFP_name.Trim() + "/" + ipMetadata.implementing_partner.abbreviation.Trim() + "/Contract/";

            string fileName = System.IO.Path.GetFileName(e.FileName);

            System.IO.Directory.CreateDirectory(Server.MapPath(defaultDirectory + createDirectory));

            //Check if a file exists in the created/existing directory.If there is one, proceed to delete
            if (System.IO.Directory.GetFiles(Server.MapPath(defaultDirectory + createDirectory)).Length != 0)
            {
                //Delete the files
                Array.ForEach(System.IO.Directory.GetFiles(Server.MapPath(defaultDirectory + createDirectory)), System.IO.File.Delete);
            }

            ContractFileUpload.SaveAs(Server.MapPath(defaultDirectory + createDirectory) + fileName);

            implementing_partner_metadata _ipMetadata = db.implementing_partner_metadata.SingleOrDefault(data => data.ip_id == ipID && data.RFP_id == rfpID);
            _ipMetadata.contract = createDirectory + fileName;
            db.SaveChanges();
        }

        protected void WorkPlanFileUpload_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
        {
            //Threading.Sleep is invoked to cause the delay so we can see the progress image when the file is uploaded
            System.Threading.Thread.Sleep(5000);

            string defaultDirectory = "~/Document_Upload/";

            var ipMetadata = (from data in db.implementing_partner_metadata
                              where data.ip_id == ipID && data.RFP_id == rfpID
                              select data).Single();

            string createDirectory = ipMetadata.RFP.RFP_name.Trim() + "/" + ipMetadata.implementing_partner.abbreviation.Trim() + "/WorkPlan/";

            string fileName = System.IO.Path.GetFileName(e.FileName);

            System.IO.Directory.CreateDirectory(Server.MapPath(defaultDirectory + createDirectory));

            //Check if a file exists in the created/existing directory.If there is one, proceed to delete
            if (System.IO.Directory.GetFiles(Server.MapPath(defaultDirectory + createDirectory)).Length != 0)
            {
                //Delete the files
                Array.ForEach(System.IO.Directory.GetFiles(Server.MapPath(defaultDirectory + createDirectory)), System.IO.File.Delete);
            }

            WorkPlanFileUpload.SaveAs(Server.MapPath(defaultDirectory + createDirectory) + fileName);

            implementing_partner_metadata _ipMetadata = db.implementing_partner_metadata.SingleOrDefault(data => data.ip_id == ipID && data.RFP_id == rfpID);
            _ipMetadata.workplan = createDirectory + fileName;
            db.SaveChanges();
        }

        protected void BudgetFileUpload_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
        {
            //Threading.Sleep is invoked to cause the delay so we can see the progress image when the file is uploaded
            System.Threading.Thread.Sleep(5000);

            string defaultDirectory = "~/Document_Upload/";

            var ipMetadata = (from data in db.implementing_partner_metadata
                              where data.ip_id == ipID && data.RFP_id == rfpID
                              select data).Single();

            string createDirectory = ipMetadata.RFP.RFP_name.Trim() + "/" + ipMetadata.implementing_partner.abbreviation.Trim() + "/Budget/";

            string fileName = System.IO.Path.GetFileName(e.FileName);

            System.IO.Directory.CreateDirectory(Server.MapPath(defaultDirectory + createDirectory));

            //Check if a file exists in the created/existing directory.If there is one, proceed to delete
            if (System.IO.Directory.GetFiles(Server.MapPath(defaultDirectory + createDirectory)).Length != 0)
            {
                //Delete the files
                Array.ForEach(System.IO.Directory.GetFiles(Server.MapPath(defaultDirectory + createDirectory)), System.IO.File.Delete);
            }

            BudgetFileUpload.SaveAs(Server.MapPath(defaultDirectory + createDirectory) + fileName);

            implementing_partner_metadata _ipMetadata = db.implementing_partner_metadata.SingleOrDefault(data => data.ip_id == ipID && data.RFP_id == rfpID);
            _ipMetadata.budget = createDirectory + fileName;
            db.SaveChanges();
        }

        protected void LogFrameFileUpload_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
        {
            //Threading.Sleep is invoked to cause the delay so we can see the progress image when the file is uploaded
            System.Threading.Thread.Sleep(5000);

            string defaultDirectory = "~/Document_Upload/";

            var ipMetadata = (from data in db.implementing_partner_metadata
                              where data.ip_id == ipID && data.RFP_id == rfpID
                              select data).Single();

            string createDirectory = ipMetadata.RFP.RFP_name.Trim() + "/" + ipMetadata.implementing_partner.abbreviation.Trim() + "/LogFrame/";

            string fileName = System.IO.Path.GetFileName(e.FileName);

            System.IO.Directory.CreateDirectory(Server.MapPath(defaultDirectory + createDirectory));

            //Check if a file exists in the created/existing directory.If there is one, proceed to delete
            if (System.IO.Directory.GetFiles(Server.MapPath(defaultDirectory + createDirectory)).Length != 0)
            {
                //Delete the files
                Array.ForEach(System.IO.Directory.GetFiles(Server.MapPath(defaultDirectory + createDirectory)), System.IO.File.Delete);
            }

            LogFrameFileUpload.SaveAs(Server.MapPath(defaultDirectory + createDirectory) + fileName);

            implementing_partner_metadata _ipMetadata = db.implementing_partner_metadata.SingleOrDefault(data => data.ip_id == ipID && data.RFP_id == rfpID);
            _ipMetadata.logframe = createDirectory + fileName;
            db.SaveChanges();
        }

        protected void RegCertFileUpload_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
        {
            //Threading.Sleep is invoked to cause the delay so we can see the progress image when the file is uploaded
            System.Threading.Thread.Sleep(5000);

            string defaultDirectory = "~/Document_Upload/";

            var ipMetadata = (from data in db.implementing_partner_metadata
                              where data.ip_id == ipID && data.RFP_id == rfpID
                              select data).Single();

            string createDirectory = ipMetadata.RFP.RFP_name.Trim() + "/" + ipMetadata.implementing_partner.abbreviation.Trim() + "/RegCert/";

            string fileName = System.IO.Path.GetFileName(e.FileName);

            System.IO.Directory.CreateDirectory(Server.MapPath(defaultDirectory + createDirectory));

            //Check if a file exists in the created/existing directory.If there is one, proceed to delete
            if (System.IO.Directory.GetFiles(Server.MapPath(defaultDirectory + createDirectory)).Length != 0)
            {
                //Delete the files
                Array.ForEach(System.IO.Directory.GetFiles(Server.MapPath(defaultDirectory + createDirectory)), System.IO.File.Delete);
            }

            RegCertFileUpload.SaveAs(Server.MapPath(defaultDirectory + createDirectory) + fileName);

            implementing_partner_metadata _ipMetadata = db.implementing_partner_metadata.SingleOrDefault(data => data.ip_id == ipID && data.RFP_id == rfpID);
            _ipMetadata.registration_certificate = createDirectory + fileName;
            db.SaveChanges();
        }

        protected void FaceFormFileUpload_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
        {
            //Threading.Sleep is invoked to cause the delay so we can see the progress image when the file is uploaded
            System.Threading.Thread.Sleep(5000);

            string defaultDirectory = "~/Document_Upload/";

            var ipMetadata = (from data in db.implementing_partner_metadata
                              where data.ip_id == ipID && data.RFP_id == rfpID
                              select data).Single();

            string createDirectory = ipMetadata.RFP.RFP_name.Trim() + "/" + ipMetadata.implementing_partner.abbreviation.Trim() + "/FaceForm/";

            string fileName = System.IO.Path.GetFileName(e.FileName);

            System.IO.Directory.CreateDirectory(Server.MapPath(defaultDirectory + createDirectory));

            //Check if a file exists in the created/existing directory.If there is one, proceed to delete
            if (System.IO.Directory.GetFiles(Server.MapPath(defaultDirectory + createDirectory)).Length != 0)
            {
                //Delete the files
                Array.ForEach(System.IO.Directory.GetFiles(Server.MapPath(defaultDirectory + createDirectory)), System.IO.File.Delete);
            }

            FaceFormFileUpload.SaveAs(Server.MapPath(defaultDirectory + createDirectory) + fileName);

            implementing_partner_metadata _ipMetadata = db.implementing_partner_metadata.SingleOrDefault(data => data.ip_id == ipID && data.RFP_id == rfpID);
            _ipMetadata.faceform = createDirectory + fileName;
            db.SaveChanges();
        }

        protected void SpecSignFileUpload_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
        {
            //Threading.Sleep is invoked to cause the delay so we can see the progress image when the file is uploaded
            System.Threading.Thread.Sleep(5000);

            string defaultDirectory = "~/Document_Upload/";

            var ipMetadata = (from data in db.implementing_partner_metadata
                              where data.ip_id == ipID && data.RFP_id == rfpID
                              select data).Single();

            string createDirectory = ipMetadata.RFP.RFP_name.Trim() + "/" + ipMetadata.implementing_partner.abbreviation.Trim() + "/SpecSign/";

            string fileName = System.IO.Path.GetFileName(e.FileName);

            System.IO.Directory.CreateDirectory(Server.MapPath(defaultDirectory + createDirectory));

            //Check if a file exists in the created/existing directory.If there is one, proceed to delete
            if (System.IO.Directory.GetFiles(Server.MapPath(defaultDirectory + createDirectory)).Length != 0)
            {
                //Delete the files
                Array.ForEach(System.IO.Directory.GetFiles(Server.MapPath(defaultDirectory + createDirectory)), System.IO.File.Delete);
            }

            SpecSignFileUpload.SaveAs(Server.MapPath(defaultDirectory + createDirectory) + fileName);

            implementing_partner_metadata _ipMetadata = db.implementing_partner_metadata.SingleOrDefault(data => data.ip_id == ipID && data.RFP_id == rfpID);
            _ipMetadata.specimen_signature = createDirectory + fileName;
            db.SaveChanges();
        }

        protected void VendorFormFileUpload_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
        {
            //Threading.Sleep is invoked to cause the delay so we can see the progress image when the file is uploaded
            System.Threading.Thread.Sleep(5000);

            string defaultDirectory = "~/Document_Upload/";

            var ipMetadata = (from data in db.implementing_partner_metadata
                              where data.ip_id == ipID && data.RFP_id == rfpID
                              select data).Single();

            string createDirectory = ipMetadata.RFP.RFP_name.Trim() + "/" + ipMetadata.implementing_partner.abbreviation.Trim() + "/VendorForm/";

            string fileName = System.IO.Path.GetFileName(e.FileName);

            System.IO.Directory.CreateDirectory(Server.MapPath(defaultDirectory + createDirectory));

            //Check if a file exists in the created/existing directory.If there is one, proceed to delete
            if (System.IO.Directory.GetFiles(Server.MapPath(defaultDirectory + createDirectory)).Length != 0)
            {
                //Delete the files
                Array.ForEach(System.IO.Directory.GetFiles(Server.MapPath(defaultDirectory + createDirectory)), System.IO.File.Delete);
            }

            VendorFormFileUpload.SaveAs(Server.MapPath(defaultDirectory + createDirectory) + fileName);

            implementing_partner_metadata _ipMetadata = db.implementing_partner_metadata.SingleOrDefault(data => data.ip_id == ipID && data.RFP_id == rfpID);
            _ipMetadata.vendor_form = createDirectory + fileName;
            db.SaveChanges();
        }

        
    }
}